package com.starsea.book.ui.newpage.fragment

import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.viewpager.widget.ViewPager
import com.starsea.book.R
import com.starsea.book.base.VMBaseFragment
import com.starsea.book.databinding.FragmentBookRanksParentBinding
import com.starsea.book.ui.newpage.entity.BookTabTitleEntity
import com.starsea.book.ui.newpage.entity.CategoryEntity
import com.starsea.book.ui.newpage.viewmodel.HomeViewModel
import com.starsea.book.utils.viewbindingdelegate.viewBinding
import java.io.Serializable

/**
 * @author Lee
 * @createTime 2024-07-12 19 GMT+8
 * @desc :
 */
class BookRanksParentFragment :
    VMBaseFragment<HomeViewModel>(R.layout.fragment_book_ranks_parent) {

    private var shopAdapter: BusinessListAdapter? = null

    var value: String = ""

    private val binding by viewBinding(FragmentBookRanksParentBinding::bind)
    override val viewModel by viewModels<HomeViewModel>()
    override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {

        toolsList = arrayListOf()
        inflater = LayoutInflater.from(requireContext())

        toolsList = arguments?.getSerializable("rankList") as MutableList<BookTabTitleEntity>
        value = arguments?.getString("value", "") ?: ""

        shopAdapter = BusinessListAdapter(childFragmentManager, value, toolsList!!)
        binding.goodsPager.adapter = shopAdapter
        binding.goodsPager.addOnPageChangeListener(onPageChangeListener)
        showToolsView(toolsList!!)
    }

    companion object {
        fun newInstance(
            value: String,
            rankList: Serializable
        ): BookRanksParentFragment {
            val args = Bundle()
            val fragment = BookRanksParentFragment()
            args.putString("value", value)
            args.putSerializable("rankList", rankList)
            fragment.arguments = args
            return fragment
        }
    }


    var toolsList: MutableList<BookTabTitleEntity>? = null
    var toolsTextViews: MutableList<TextView>? = null
    var views: MutableList<View>? = null
    var viewLine: MutableList<View>? = null
    var inflater: LayoutInflater? = null
    var scrllViewWidth = 0
    var scrollViewMiddlee: Int = 0
    var currentItem = 0
    val position = 0


    private val onPageChangeListener: ViewPager.OnPageChangeListener = object :
        ViewPager.OnPageChangeListener {
        override fun onPageSelected(arg0: Int) {
            if (binding.goodsPager.currentItem !== arg0) binding.goodsPager.setCurrentItem(arg0)
            if (currentItem != arg0) {
                changeTextColor(arg0)
                changeTextLocation(arg0)
            }
            currentItem = arg0
        }

        override fun onPageScrolled(arg0: Int, arg1: Float, arg2: Int) {}
        override fun onPageScrollStateChanged(arg0: Int) {}
    }

    private val toolsItemListener =
        View.OnClickListener { v: View ->
            binding.goodsPager.setCurrentItem(
                v.id,
                false
            )
        }

    private fun showToolsView(entity: List<BookTabTitleEntity>) {
        val toolsLayout = binding.tools
        toolsTextViews = arrayListOf()
        viewLine = arrayListOf()
        views = arrayListOf()
        for (i in toolsList!!.indices) {

            val view: View = inflater!!.inflate(R.layout.item_category_left, null)
            view.id = i
            view.setOnClickListener(toolsItemListener)
            val textView = view.findViewById<View>(R.id.text) as TextView
            val viewLine = view.findViewById<View>(R.id.view_line) as View
            textView.text = toolsList!![i].name
            textView.setSingleLine()
            textView.maxEms = 5
            textView.ellipsize = TextUtils.TruncateAt.END
            toolsLayout.addView(view)
            toolsTextViews!!.add(textView)
            this.viewLine!!.add(viewLine)
            views!!.add(view)
        }
        changeTextColor(position)
        binding.goodsPager.setCurrentItem(position, false)
    }


    class BusinessListAdapter(
        fm: FragmentManager?,
        var value: String,
        var toolsList: MutableList<BookTabTitleEntity>
    ) :
        FragmentPagerAdapter(fm!!) {
        override fun getItem(arg0: Int): Fragment {
            val fragment: Fragment = BookRanksChildFragment()
            val bundle = Bundle()
            bundle.putString("typeId", value)
            bundle.putString("rankType", toolsList[arg0].value)
            fragment.arguments = bundle
            return fragment
        }

        override fun getCount(): Int {
            return toolsList.size
        }
    }


    private fun changeTextColor(id: Int) {
        for (i in toolsTextViews!!.indices) {
            if (i != id) {
                views!![i].setBackgroundColor(Color.TRANSPARENT)
                toolsTextViews!![i].setTextColor(Color.parseColor("#969696"))
                toolsTextViews!![i].paint.isFakeBoldText = true
                viewLine!![i].visibility = View.INVISIBLE
            }
        }
        viewLine!![id].visibility = View.VISIBLE
        views!![id].setBackgroundResource(R.color.transparent)
        toolsTextViews!![id].setTextColor(Color.parseColor("#8D4BBB"))
        toolsTextViews!![id].paint.isFakeBoldText = true
    }

    private fun changeTextLocation(clickPosition: Int) {
        val x = views!![clickPosition].top - getScrollViewMiddle() + getViewheight(
            views!![clickPosition]
        ) / 2
        binding.toolsScrlllview.smoothScrollTo(0, x)
    }

    private fun getScrollViewMiddle(): Int {
        if (scrollViewMiddlee == 0) scrollViewMiddlee = getScrollViewheight() / 2
        return scrollViewMiddlee
    }

    private fun getScrollViewheight(): Int {
        if (scrllViewWidth == 0) scrllViewWidth =
            binding.toolsScrlllview.getBottom() - binding.toolsScrlllview.getTop()
        return scrllViewWidth
    }

    private fun getViewheight(view: View): Int {
        return view.bottom - view.top
    }

}